
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;
using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Data;

namespace dpatoolkit.UILayer
{

    /// <summary>
    /// Summary description for MetaInfoForm.
    /// </summary>
    public class MetaInfoEditor : System.Windows.Forms.Form
    {
        private System.Data.DataTable metaInfoDataTable;
        private System.Windows.Forms.Button editLangButton;
        private System.Windows.Forms.TabControl tabControl;
        private System.Data.DataSet metaInfoDataSet;
        private System.Data.DataColumn dataColumn2;
        private System.Windows.Forms.Button addLangButton;
        private System.Windows.Forms.Button remButton;
        private System.Data.DataColumn dataColumn1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button okButton;
        #region Fields

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        #endregion

        #region constructors,dispose
        public MetaInfoEditor(string metainfo)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            if (metainfo == null)
                return;
            if (metainfo.Length == 0)
                return;

            try
            {
                XmlDataDocument dataDoc = new XmlDataDocument(this.metaInfoDataSet);
                dataDoc.LoadXml(metainfo);
                InitTabs();

            }
            catch (Exception exp)
            {
                ErrorMessage.Show("Error while getting the metainfo:\n" + exp.ToString(), "Error");
            }

        }

        public MetaInfoEditor(Hashtable metaInfoMap)
        {
            this.InitializeComponent();
            if (metaInfoMap == null) return;
            try
            {
                foreach (string lang in metaInfoMap.Keys)
                    this.AddMetaInfo(lang, metaInfoMap[lang] as string);
            }
            catch (Exception exp)
            {
                Console.Error.WriteLine("Following encountered in MetaInfoEditor while trying to instantiate from the hashtable constructor:" + exp.ToString(), "Error");
            }

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.metaInfoDataSet.Dispose();
                if (components != null)
                {
                    components.Dispose();
                }

            }
            base.Dispose(disposing);
        }

        #endregion

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.okButton = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.dataColumn1 = new System.Data.DataColumn();
            this.remButton = new System.Windows.Forms.Button();
            this.addLangButton = new System.Windows.Forms.Button();
            this.dataColumn2 = new System.Data.DataColumn();
            this.metaInfoDataSet = new System.Data.DataSet();
            this.metaInfoDataTable = new System.Data.DataTable();
            this.tabControl = new System.Windows.Forms.TabControl();
            this.editLangButton = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.metaInfoDataSet)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.metaInfoDataTable)).BeginInit();
            this.SuspendLayout();
            // 
            // okButton
            // 
            this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.okButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.okButton.Location = new System.Drawing.Point(560, 360);
            this.okButton.Name = "okButton";
            this.okButton.Size = new System.Drawing.Size(88, 24);
            this.okButton.TabIndex = 4;
            this.okButton.Text = "&DONE";
            this.okButton.Click += new System.EventHandler(this.okButton_Click);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(674, 30);
            this.label1.TabIndex = 5;
            this.label1.Text = "The MetaInfo is not displayed in the class diagram but in the hover sense message" +
                ", the code generation plug-ins can query the meta info for a language and custom" +
                "ize the code generation, ";
            // 
            // dataColumn1
            // 
            this.dataColumn1.AllowDBNull = false;
            this.dataColumn1.ColumnName = "Language";
            // 
            // remButton
            // 
            this.remButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.remButton.Location = new System.Drawing.Point(272, 45);
            this.remButton.Name = "remButton";
            this.remButton.Size = new System.Drawing.Size(120, 21);
            this.remButton.TabIndex = 2;
            this.remButton.Text = "&Remove Language";
            this.remButton.Click += new System.EventHandler(this.remButton_Click);
            // 
            // addLangButton
            // 
            this.addLangButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.addLangButton.Location = new System.Drawing.Point(16, 45);
            this.addLangButton.Name = "addLangButton";
            this.addLangButton.Size = new System.Drawing.Size(104, 21);
            this.addLangButton.TabIndex = 0;
            this.addLangButton.Text = "&New Language";
            this.addLangButton.Click += new System.EventHandler(this.addLangButton_Click);
            // 
            // dataColumn2
            // 
            this.dataColumn2.AllowDBNull = false;
            this.dataColumn2.ColumnName = "Info";
            // 
            // metaInfoDataSet
            // 
            this.metaInfoDataSet.DataSetName = "MetaInfo";
            this.metaInfoDataSet.Locale = new System.Globalization.CultureInfo("en-US");
            this.metaInfoDataSet.Tables.AddRange(new System.Data.DataTable[] {
													 this.metaInfoDataTable});
            // 
            // metaInfoDataTable
            // 
            this.metaInfoDataTable.Columns.AddRange(new System.Data.DataColumn[] {
													     this.dataColumn1,
													     this.dataColumn2});
            this.metaInfoDataTable.Constraints.AddRange(new System.Data.Constraint[] {
														 new System.Data.UniqueConstraint("Constraint1", new string[] {
																						      "Language"}, true)});
            this.metaInfoDataTable.PrimaryKey = new System.Data.DataColumn[] {
													 this.dataColumn1};
            this.metaInfoDataTable.TableName = "Languages";
            // 
            // tabControl
            // 
            this.tabControl.HotTrack = true;
            this.tabControl.Location = new System.Drawing.Point(16, 74);
            this.tabControl.Name = "tabControl";
            this.tabControl.SelectedIndex = 0;
            this.tabControl.Size = new System.Drawing.Size(640, 270);
            this.tabControl.TabIndex = 3;
            // 
            // editLangButton
            // 
            this.editLangButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.editLangButton.Location = new System.Drawing.Point(136, 45);
            this.editLangButton.Name = "editLangButton";
            this.editLangButton.Size = new System.Drawing.Size(120, 21);
            this.editLangButton.TabIndex = 1;
            this.editLangButton.Text = "&Edit Language";
            this.editLangButton.Click += new System.EventHandler(this.editLangButton_Click);
            // 
            // MetaInfoEditor
            // 
            this.AcceptButton = this.okButton;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(674, 398);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.editLangButton);
            this.Controls.Add(this.tabControl);
            this.Controls.Add(this.okButton);
            this.Controls.Add(this.remButton);
            this.Controls.Add(this.addLangButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "MetaInfoEditor";
            this.Text = "Meta Info Editor";
            ((System.ComponentModel.ISupportInitialize)(this.metaInfoDataSet)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.metaInfoDataTable)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        #region GUI related, event handlers

        private void InitTabs()
        {


            DataTable table = this.metaInfoDataSet.Tables[MetaInfoNames.Languages];
            foreach (DataRow row in table.Rows)
            {
                TextTabPage page = new TextTabPage((string)row[MetaInfoNames.Language]);
                page.MetaInfo = (string)row[MetaInfoNames.Info];
                this.tabControl.Controls.Add(page);

            }

        }


        private void addLangButton_Click(object sender, System.EventArgs e)
        {

            InputForm form = new InputForm("Language Name", "Enter Language name");
            if (form.ShowDialog() == DialogResult.Cancel)
                return;
            string lang = form.Input;
            if (lang.Length == 0)
            {
                ErrorMessage.Show("Language Name cannot be of zero length!", "error");
                return;
            }
            try
            {
                this.AddMetaInfo(lang, string.Empty);
                TextTabPage page = new TextTabPage(lang);
                page.MetaInfo = string.Empty;
                this.tabControl.Controls.Add(page);
            }
            catch (Exception exp)
            {
                ErrorMessage.Show("This language already exists!\n" + exp.Message, "Error");
            }
        }

        private void remButton_Click(object sender, System.EventArgs e)
        {
            if (this.tabControl.TabCount == 0) return;
            string lang = this.tabControl.SelectedTab.Text;
            this.RemoveMetaInfo(lang);
            this.tabControl.Controls.Remove(this.tabControl.SelectedTab);
        }




        private void UpdateDataSet()
        {
            foreach (TextTabPage tpage in this.tabControl.Controls)
            {
                try
                {
                    DataRow row = this.metaInfoDataTable.Rows.Find(tpage.Text);
                    row[MetaInfoNames.Info] = tpage.MetaInfo;
                }
                catch (Exception exp)
                {
                    ErrorMessage.Show("The following exp :" + exp.ToString(), "error");
                }
            }
        }


        private void okButton_Click(object sender, System.EventArgs e)
        {
            this.UpdateDataSet();
        }

        private void editLangButton_Click(object sender, System.EventArgs e)
        {
            if (this.tabControl.TabCount == 0) return;
            InputForm form = new InputForm("Language", "Edit Language Name");
            form.Input = this.tabControl.SelectedTab.Text;
            if (form.ShowDialog() == DialogResult.Cancel) return;
            if (form.Input.Length == 0) return;
            try
            {

                DataRow row = this.metaInfoDataTable.Rows.Find(this.tabControl.SelectedTab.Text);
                if (row == null) return;
                row[MetaInfoNames.Language] = form.Input;
                this.tabControl.SelectedTab.Text = form.Input;

            }
            catch (Exception exp)
            {
                ErrorMessage.Show("This MetaInfo for this Language already exists!\n" + exp.Message, "error");
            }

        }
        #endregion

        public string MetaInfo
        {
            get
            {
                return this.metaInfoDataSet.GetXml();
            }
            set
            {
                //currently not required, handled by the first constructor.
            }
        }


        public void AddMetaInfo(string lang, string minfo)
        {
            DataRow row = this.metaInfoDataTable.Rows.Find(lang);
            if (row != null)
            {
                string temp = (string)row[MetaInfoNames.Info];
                temp += " " + minfo;
                row[MetaInfoNames.Info] = temp;
                row.AcceptChanges();
                return;
            }
            row = this.metaInfoDataTable.NewRow();
            row[MetaInfoNames.Language] = lang;
            row[MetaInfoNames.Info] = minfo;
            this.metaInfoDataTable.Rows.Add(row);

        }

        public void RemoveMetaInfo(string lang)
        {
            DataRow row = this.metaInfoDataTable.Rows.Find(lang);
            if (row != null)
                metaInfoDataTable.Rows.Remove(row);

        }
        public string GetMetaInfo(string lang)
        {
            DataRow row = this.metaInfoDataTable.Rows.Find(lang);
            if (row == null) return "";
            return (string)row[MetaInfoNames.Info];
        }

        public Hashtable GetAllMetaInfo()
        {
            Hashtable allmetainfo = new Hashtable();
            foreach (DataRow row in this.metaInfoDataTable.Rows)
            {
                allmetainfo.Add((string)row[MetaInfoNames.Language],
                    (string)row[MetaInfoNames.Info]);
            }
            return allmetainfo;

        }

        public override string ToString()
        {
            string str = "";
            foreach (DataRow row in this.metaInfoDataTable.Rows)
            {
                str += "[" + (string)row[MetaInfoNames.Language] + "]" + (string)row[MetaInfoNames.Info] + "\n";
            }
            return str;

        }

        private void MetaInfoEditor_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBox.Show("In Closing");
            this.UpdateDataSet();
        }
    }


    public class MetaInfoNames
    {
        public static string Languages = "Languages",
            Info = "Info",
            Language = "Language";
    }

}